package com.medvalley.tcm.service.impl;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.base.EmptyException;
import com.medvalley.tcm.dao.*;
import com.medvalley.tcm.domain.*;
import com.medvalley.tcm.pojo.SickPojo;
import com.medvalley.tcm.service.ISickService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class SickInfoService implements ISickService {

    @Autowired
    private SickInfoDao sickInfoDao;

    @Autowired
    private SickUserDao sickUserDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private AppointmentDao appointmentDao;

    @Autowired
    private WenZhenDao wenZhenDao;

    @Override
    public SickPojo saveSickInfo(SickPojo sickPojo) {
        String barCode = sickPojo.getBarCode();
        SickInfo oldSickInfo = sickInfoDao.findOneByBarCode(barCode);
        if (oldSickInfo != null) {
            throw new BusinessException("该条形码已经提交过数据");
        }
        SickInfo sickInfo = new SickInfo();
        BeanUtils.copyProperties(sickPojo, sickInfo);
        Date now = new Date();
        sickInfo.setCreateTime(now);
        sickInfo.setUpdateTime(now);
        sickInfoDao.save(sickInfo);
        sickPojo.setId(sickInfo.getId());
        // 2020-03-30 青岛河北需求
        if (sickPojo.getDiagnosticMechanismID() == 29) {
            WenZhen wenZhen = new WenZhen();
            wenZhen.setSickName(sickPojo.getPatientName());
            wenZhen.setSickPhone(sickPojo.getPatientPhone());
            wenZhen.setBarcode(sickPojo.getBarCode());
            wenZhen.setAge(sickPojo.getPatientAge());
            wenZhen.setBirthday(DateFormatUtils.format(sickPojo.getPatientBirth(), "yyyy-MM-dd"));
            wenZhen.setGender(sickPojo.getPatientSex());
            wenZhen.setMedicalId(sickInfo.getId());
            wenZhenDao.save(wenZhen);
        }
        return sickPojo;
    }

    @Override
    public SickPojo updateSickInfo(SickPojo sickPojo) {
        SickInfo sickInfo = sickInfoDao.findById(sickPojo.getId()).orElseThrow(() -> new BusinessException("没有此病例"));
        BeanUtils.copyProperties(sickPojo, sickInfo, "createTime", "id");
        Date now = new Date();
        sickInfo.setUpdateTime(now);
        sickInfoDao.save(sickInfo);
        return sickPojo;
    }

    @Override
    public SickPojo getSickInfoById(long id) {
        SickInfo sickInfo = sickInfoDao.findById(id).orElse(null);
        if (sickInfo == null) {
            throw new EmptyException(" 没有相关病历 ");
        }
        SickPojo sickPojo = new SickPojo();
        BeanUtils.copyProperties(sickInfo, sickPojo);
        return sickPojo;
    }

    @Override
    public SickPojo getSickInfoByBarCode(String barCode) {
        SickInfo sickInfo = sickInfoDao.findOneByBarCode(barCode);
        if (sickInfo == null) {
            throw new EmptyException(" 没有相关病历 ");
        }
        SickPojo sickPojo = new SickPojo();
        BeanUtils.copyProperties(sickInfo, sickPojo);
        return sickPojo;
    }

    @Override
    public List<SickPojo> getSickInfoByIdNum(String idNum) {
        List<SickInfo> sickInfos = sickInfoDao.findAllByPatientCardNum(idNum);
        if (CollectionUtils.isEmpty(sickInfos)) {
            throw new EmptyException(" 没有相关病历 ");
        }
        List<SickPojo> sickPojos = Lists.newArrayList();
        sickInfos.forEach(e -> {
            SickPojo sickPojo = new SickPojo();
            BeanUtils.copyProperties(e, sickPojo);
            sickPojos.add(sickPojo);
        });

        SickPojo sickPojo = new SickPojo();
        BeanUtils.copyProperties(sickInfos, sickPojo);
        return sickPojos;
    }

    @Override
    @Transactional
    public SickPojo finishReport(long id) {
        SickInfo sickInfo = sickInfoDao.findById(id).orElseThrow(() -> new BusinessException(" 无此报告 "));
        sickInfo.setIsPrint(1);
        SickPojo sickPojo = new SickPojo();
        BeanUtils.copyProperties(sickInfo, sickPojo);
        sickInfoDao.save(sickInfo);
        String phone = sickInfo.getPatientPhone();
        UserInfo userInfo = userInfoDao.findOneByPhone(phone);
        if (userInfo != null) {
            SickAndUser sickAndUser = sickUserDao.findOneBySickIdAndUserId(sickInfo.getId(), userInfo.getId());
            if (sickAndUser == null) {
                SickAndUser tempSickAndUser = new SickAndUser();
                tempSickAndUser.setSickId(id);
                tempSickAndUser.setUserId(userInfo.getId());
                tempSickAndUser.setPhone(phone);
                tempSickAndUser.setSickCreateTime(sickInfo.getCreateTime());
                sickUserDao.save(tempSickAndUser);
            }
        }
        // 更新预约状态
        String barcode = sickInfo.getBarCode();
        Appointment appointment = appointmentDao.findOneByBarcode(barcode);
        if (appointment != null) {
            appointment.setStatus(1);// 更新为已使用
            appointmentDao.save(appointment);
        }
        return sickPojo;
    }

}
